$(function () {
    $('#tuichu').on('click', function () {
        layer.confirm('退出?', { icon: 3, title: '提示' }, function (index) {
            localStorage.removeItem('token');
            location.href = '/login.html'
            layer.close(index);
        });
    })
    var layer = layui.layer
    getUserInfo()
    //获取用户信息
    function getUserInfo() {
        $.ajax({
            type: 'GET',
            url: '/my/userinfo',
            success: function (res) {
                console.log(res);

                if (res.code !== 0) {
                    return layer.msg('获取用户信息失败')
                }
                layer.msg('获取用户信息成功')
                renderAvatar(res.data)
            }
        })
    }
    //渲染用户头像
    function renderAvatar(user) {
        var name = user.nickname || user.username
        if (user.user_pic !== null) {
            //shang
            $('#header-avatar').html(`<img src="${user.user_pic}" class="layui-nav-img">个人中心`);
            //zuo
            $('.user-info-box').html(`<img src="${user.user_pic}" class="layui-nav-img">
            <span class="welcome">&nbsp;欢迎&nbsp; ${name}</span>`)
        } else {
            var firstChar = name.charAt(0).toUpperCase()
            //shang
            $('#header-avatar').html(`
      <div class="text-avatar">${firstChar}</div>
      个人中心`)
            // zuo
            $('.user-info-box').html(`
      <div class="text-avatar">${firstChar}</div>
      <span class="welcome">&nbsp;欢迎&nbsp; ${name}</span>`)
        }
    }
})